package com.cloudweb.oa.controller;

import cn.js.fan.db.ListResult;
import cn.js.fan.mail.SendMail;
import cn.js.fan.util.*;
import cn.js.fan.web.Global;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.utils.I18nUtil;
import com.cloudweb.oa.vo.Result;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.dept.DeptDb;
import com.redmoon.oa.flow.*;
import com.redmoon.oa.message.MessageDb;
import com.redmoon.oa.person.UserDb;
import com.redmoon.oa.person.UserProxyDb;
import com.redmoon.oa.person.UserProxyMgr;
import com.redmoon.oa.sms.IMsgUtil;
import com.redmoon.oa.sms.SMSFactory;
import com.redmoon.oa.ui.LocalUtil;
import com.redmoon.oa.visual.ModulePrivDb;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Iterator;

@RestController
@RequestMapping("/user/proxy")
public class UserProxyController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    AuthUtil authUtil;

    @Autowired
    I18nUtil i18nUtil;

    @ApiOperation(value = "列出代理", notes = "列出代理", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> list() {
        UserDb ud = new UserDb();
        UserProxyDb upd = new UserProxyDb();
        String sql = "select id from " + upd.getTable().getName() + " where user_name=? order by id desc";
        Iterator ir = upd.list(sql, new Object[]{authUtil.getUserName()}).iterator();
        JSONArray list = new JSONArray();
        while (ir.hasNext()) {
            upd = (UserProxyDb) ir.next();
            String proxy = StrUtil.getNullStr(upd.getString("proxy"));
            String proxyUserRealName = "";
            if (!"".equals(proxy)) {
                proxyUserRealName = ud.getUserDb(proxy).getRealName();
                if (proxyUserRealName == null) {
                    proxyUserRealName = "";
                }
            }

            JSONObject json = new JSONObject();
            json.put("id", upd.getInt("id"));
            json.put("realName", ud.getRealName());
            json.put("proxyUserRealName", proxyUserRealName);
            json.put("proxy", proxy);

            String deptNames = "";
            String depts = "";
            if (upd.getInt("proxy_type") == UserProxyDb.TYPE_DEPT) {
                String[] arydepts = StrUtil.split(upd.getString("starter_param"), ",");
                if (arydepts != null) {
                    DeptDb dd = new DeptDb();
                    for (String arydept : arydepts) {
                        if ("".equals(depts)) {
                            depts = arydept;
                            dd = dd.getDeptDb(arydept);
                            deptNames = dd.getName();
                        } else {
                            depts += "," + arydept;
                            dd = dd.getDeptDb(arydept);
                            deptNames += "," + dd.getName();
                        }
                    }
                }
            }
            json.put("deptCodes", depts);
            json.put("deptNames", deptNames);
            json.put("proxyType", upd.getInt("proxy_type"));
            json.put("beginDate", DateUtil.format(upd.getDate("begin_date"), "yyyy-MM-dd HH:mm:ss"));
            json.put("endDate", DateUtil.format(upd.getDate("end_date"), "yyyy-MM-dd HH:mm:ss"));
            json.put("flowCode", upd.getString("flow_code"));
            Leaf lf = new Leaf();
            lf = lf.getLeaf(upd.getString("flow_code"));
            if (lf != null) {
                json.put("flowName", lf.getName());
            }

            String realNames = "";
            if (!"".equals(StrUtil.getNullStr(upd.getString("person_related")))) {
                String[] ary = StrUtil.split(upd.getString("person_related"), ",");
                if (ary != null) {
                    UserDb usrd = new UserDb();
                    for (String s : ary) {
                        if ("".equals(realNames)) {
                            usrd = usrd.getUserDb(s);
                            realNames = usrd.getRealName();
                        } else {
                            usrd = usrd.getUserDb(s);
                            realNames += "," + usrd.getRealName();
                        }
                    }
                }
            }
            json.put("personRelated", upd.getString("person_related"));
            json.put("personRealNameRelated", realNames);
            list.add(json);
        }

        return new Result<>(list);
    }

    @ApiOperation(value = "删除代理", notes = "删除代理", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> del(@RequestParam Integer id) {
        UserProxyDb upd = new UserProxyDb();
        upd = (UserProxyDb) upd.getQObjectDb(id);
        String proxy = upd.getString("proxy");
        String users = upd.getString("person_related");
        String fromUserName = upd.getString("user_Name");
        String beginDate = upd.getString("begin_date");
        String endDate = upd.getString("end_date");
        try {
            if (upd.del()) {
                //删除代理发送提醒
                //发送邮件提醒
                SendMail sendmail = WorkflowDb.getSendMail();
                UserDb toUserDb = new UserDb();
                toUserDb = toUserDb.getUserDb(proxy);
                UserDb formUserDb = new UserDb();
                formUserDb = formUserDb.getUserDb(fromUserName);
                String fromNick = "";
                String toNick = "";
                try {
                    fromNick = MimeUtility.encodeText(formUserDb.getRealName());
                    toNick = MimeUtility.encodeText(toUserDb.getRealName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String fromEmail = Global.getEmail();
                fromNick = fromNick + "<" + fromEmail + ">";
                String toRealName = toUserDb.getRealName();
                //2016年10月21号-2016-10-30  XX不在，工作流程由XXX代理
                com.redmoon.oa.Config cfg = new com.redmoon.oa.Config();
                String title = cfg.get("proxy_del_msg_title");
                String content = cfg.get("proxy_del_msg_content");
                content = content.replace("%fromUser", formUserDb.getRealName());
                content = content.replace("%proxy", toUserDb.getRealName());

                MessageDb md = new MessageDb();
                if (SMSFactory.isUseSMS()) {
                    IMsgUtil imu = SMSFactory.getMsgUtil();
                    if (imu != null) {
                        try {
                            imu.send(toUserDb, content, MessageDb.SENDER_SYSTEM);
                        } catch (ErrMsgException e) {
                            LogUtil.getLog(getClass()).error(e);
                        }
                    }
                }
                // 发送信息
                try {
                    md.sendSysMsg(toUserDb.getName(), title, content);
                } catch (ErrMsgException ex2) {
                    ex2.printStackTrace();
                }

                boolean flowNotifyByEmail = cfg.getBooleanProperty("flowNotifyByEmail");
                if (flowNotifyByEmail) {
                    sendmail.initMsg(toUserDb.getEmail(), fromNick, title, content, true);
                    sendmail.send();
                    sendmail.clear();
                }
                try {
                    if (!"".equals(users)) {
                        //如果相关人员不为空的话，可以实例化后，发送邮件
                        String[] ary = users.split(",");
                        for (String s : ary) {
                            toUserDb = toUserDb.getUserDb(s);
                            toNick = MimeUtility.encodeText(toUserDb.getRealName());
                            if (SMSFactory.isUseSMS()) {
                                IMsgUtil imu = SMSFactory.getMsgUtil();
                                if (imu != null) {
                                    try {
                                        imu.send(toUserDb, content, MessageDb.SENDER_SYSTEM);
                                    } catch (ErrMsgException ex1) {
                                        LogUtil.getLog(getClass()).error(ex1);
                                    }
                                }
                            }
                            // 发送信息
                            try {
                                md.sendSysMsg(toUserDb.getName(), title, content);
                            } catch (ErrMsgException ex2) {
                                ex2.printStackTrace();
                            }
                            if (flowNotifyByEmail) {
                                if (!"".equals(StrUtil.getNullStr(toUserDb.getEmail()))) {
                                    sendmail.initMsg(toUserDb.getEmail(), fromNick, title, content, true);
                                    sendmail.send();
                                    sendmail.clear();
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    LogUtil.getLog(getClass()).error(e);
                }
            }
        } catch (ResKeyException e) {
            return new Result<>(false, e.getMessage(request));
        }
        return new Result<>();
    }

    @ApiOperation(value = "设置代理", notes = "设置代理", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> update(@RequestParam Integer id, @RequestParam String proxy) {
        String userName = authUtil.getUserName();
        if (proxy.equals(userName)) {
            return new Result<>(false, i18nUtil.get("userAgent"));
        }

        String starterParam = ParamUtil.get(request, "starter_param");
        java.util.Date beginDate = DateUtil.parse(ParamUtil.get(request, "proxyBeginDate") + " " + ParamUtil.get(request, "proxyBeginTime"), "yyyy-MM-dd HH:mm:ss");
        java.util.Date endDate = DateUtil.parse(ParamUtil.get(request, "proxyEndDate") + " " + ParamUtil.get(request, "proxyEndTime"), "yyyy-MM-dd HH:mm:ss");
        int proxyType = UserProxyDb.TYPE_DEFAULT;
        if (!"".equals(starterParam)) {
            proxyType = UserProxyDb.TYPE_DEPT;
        }

        if (beginDate == null || endDate == null) {
            return new Result<>(false, i18nUtil.get("dateFormatError"));
        }

        if (DateUtil.compare(beginDate, endDate) == 1) {
            return new Result<>(false, i18nUtil.get("startDateNotEndDate"));
        }

        String flowCode = ParamUtil.get(request, "flow_code");

        UserProxyDb upd = new UserProxyDb();
        upd = (UserProxyDb) upd.getQObjectDb(id);
        boolean re = false;
        try {
            re = upd.save(new JdbcTemplate(), new Object[]{userName, proxy, beginDate, endDate, starterParam, new Integer(proxyType), new java.util.Date(), flowCode, new Integer(id)});
        } catch (SQLException e) {
            LogUtil.getLog(getClass()).error(e);
            return new Result<>(false, e.getMessage());
        }
        if (re) {
            String users = upd.getString("person_related");
            //发送邮件提醒
            //设置被代理人的邮箱地址
            SendMail sendmail = WorkflowDb.getSendMail();
            UserDb toUserDb = new UserDb();
            toUserDb = toUserDb.getUserDb(proxy);
            UserDb formUserDb = new UserDb();
            formUserDb = formUserDb.getUserDb(userName);

            String fromNick = "";
            String toNick = "";
            try {
                fromNick = MimeUtility.encodeText(formUserDb.getRealName());
                toNick = MimeUtility.encodeText(toUserDb.getRealName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            String fromEmail = Global.getEmail();
            fromNick = fromNick + "<" + fromEmail + ">";

            String toRealName = toUserDb.getRealName();
            // 2016年10月21号-2016-10-30  XX不在，工作流程由XXX代理
            com.redmoon.oa.Config cfg = new com.redmoon.oa.Config();
            String title = cfg.get("proxy_add_msg_title");
            String content = cfg.get("proxy_add_msg_content");
            content = content.replace("%beginDate", ParamUtil.get(request, "proxyBeginDate"));
            content = content.replace("%endDate", ParamUtil.get(request, "proxyEndDate"));
            content = content.replace("%fromUser", formUserDb.getRealName());
            content = content.replace("%proxy", toUserDb.getRealName());

            MessageDb md = new MessageDb();
            if (SMSFactory.isUseSMS()) {
                IMsgUtil imu = SMSFactory.getMsgUtil();
                if (imu != null) {
                    try {
                        imu.send(toUserDb, content, MessageDb.SENDER_SYSTEM);
                    } catch (ErrMsgException ex1) {
                        ex1.printStackTrace();
                    }
                }
            }
            // 发送信息
            try {
                md.sendSysMsg(toUserDb.getName(), title, content);
            } catch (ErrMsgException ex2) {
                ex2.printStackTrace();
            }

            boolean flowNotifyByEmail = cfg.getBooleanProperty("flowNotifyByEmail");
            if (flowNotifyByEmail) {
                sendmail.initMsg(toUserDb.getEmail(), fromNick, title, content, true);
                sendmail.send();
                sendmail.clear();
            }
            try {
                if (!"".equals(users)) {
                    //如果相关人员不为空的话，可以实例化后，发送邮件
                    String[] ary = users.split(",");
                    for (String s : ary) {
                        toUserDb = toUserDb.getUserDb(s);
                        toNick = MimeUtility.encodeText(toUserDb.getRealName());
                        if (SMSFactory.isUseSMS()) {
                            IMsgUtil imu = SMSFactory.getMsgUtil();
                            if (imu != null) {
                                try {
                                    imu.send(toUserDb, content, MessageDb.SENDER_SYSTEM);
                                } catch (ErrMsgException ex1) {
                                    ex1.printStackTrace();
                                }
                            }
                        }
                        // 发送信息
                        try {
                            md.sendSysMsg(toUserDb.getName(), title, content);
                        } catch (ErrMsgException ex2) {
                            ex2.printStackTrace();
                        }
                        if (flowNotifyByEmail) {
                            if (!"".equals(StrUtil.getNullStr(toUserDb.getEmail()))) {
                                sendmail.initMsg(toUserDb.getEmail(), fromNick, title, content, true);
                                sendmail.send();
                                sendmail.clear();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new Result<>(re);
    }

    @ApiOperation(value = "重置代理", notes = "重置代理", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/reset", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> reset() {
        UserProxyMgr upm = new UserProxyMgr();
        try {
            upm.resetProxy(request);
        } catch (ErrMsgException e) {
            return new Result<>(false, e.getMessage());
        }
        return new Result<>();
    }

    @ApiOperation(value = "添加代理", notes = "添加代理", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> create() {
        String userName = authUtil.getUserName();
        String proxy = ParamUtil.get(request, "proxy");
        if (proxy.equals(userName)) {
            return new Result<>(false, i18nUtil.get("userAgent"));
        }

        String users = ParamUtil.get(request, "users");
        String starterParam = ParamUtil.get(request, "starter_param");
        java.util.Date beginDate = DateUtil.parse(ParamUtil.get(request, "proxyBeginDate"), "yyyy-MM-dd HH:mm:ss");
        java.util.Date endDate = DateUtil.parse(ParamUtil.get(request, "proxyEndDate"), "yyyy-MM-dd HH:mm:ss");
        String beginDateStr = ParamUtil.get(request, "proxyBeginDate");
        String endDateStr = ParamUtil.get(request, "proxyEndDate");
        int proxyType = UserProxyDb.TYPE_DEFAULT;
        if (!"".equals(starterParam)) {
            proxyType = UserProxyDb.TYPE_DEPT;
        }

        if (beginDate == null || endDate == null) {
            return new Result<>(false, i18nUtil.get("dateFormatError"));
        }

        if (DateUtil.compare(beginDate, endDate) == 1) {
            return new Result<>(false, i18nUtil.get("startDateNotEndDate"));
        }

        String flowCode = ParamUtil.get(request, "flow_code");

        UserProxyDb upd = new UserProxyDb();
        boolean re;
        try {
            re = upd.create(new JdbcTemplate(), new Object[]{userName, proxy, beginDate, endDate, starterParam, proxyType, new java.util.Date(), flowCode, users});
        } catch (ResKeyException e) {
            return new Result<>(false, e.getMessage(request));
        }
        if (re) {
            //发送邮件提醒
            //设置被代理人的邮箱地址
            SendMail sendmail = WorkflowDb.getSendMail();
            UserDb toUserDb = new UserDb();
            toUserDb = toUserDb.getUserDb(proxy);
            //设置设置代理人的邮箱地址
            UserDb formUserDb = new UserDb();
            formUserDb = formUserDb.getUserDb(userName);
            String fromNick = "";
            String toNick = "";
            try {
                fromNick = MimeUtility.encodeText(formUserDb.getRealName());
                toNick = MimeUtility.encodeText(toUserDb.getRealName());
            } catch (Exception e) {
                LogUtil.getLog(getClass()).error(e);
            }
            String fromEmail = Global.getEmail();
            fromNick = fromNick + "<" + fromEmail + ">";
            String toRealName = toUserDb.getRealName();
            //2016年10月21号-2016-10-30  XX不在，工作流程由XXX代理
            com.redmoon.oa.Config cfg = new com.redmoon.oa.Config();
            String title = cfg.get("proxy_add_msg_title");
            String content = cfg.get("proxy_add_msg_content");
            content = content.replace("%beginDate", beginDateStr);
            content = content.replace("%endDate", endDateStr);
            content = content.replace("%fromUser", formUserDb.getRealName());
            content = content.replace("%proxy", toUserDb.getRealName());

            MessageDb md = new MessageDb();
            if (SMSFactory.isUseSMS()) {
                IMsgUtil imu = SMSFactory.getMsgUtil();
                if (imu != null) {
                    try {
                        imu.send(toUserDb, content, MessageDb.SENDER_SYSTEM);
                    } catch (ErrMsgException ex1) {
                        LogUtil.getLog(getClass()).error(ex1);
                    }
                }
            }
            // 发送信息
            try {
                md.sendSysMsg(toUserDb.getName(), title, content);
            } catch (ErrMsgException ex2) {
                LogUtil.getLog(getClass()).error(ex2);
            }
            boolean flowNotifyByEmail = cfg.getBooleanProperty("flowNotifyByEmail");
            if (flowNotifyByEmail) {
                sendmail.initMsg(toUserDb.getEmail(), fromNick, title, content, true);
                sendmail.send();
                sendmail.clear();
            }
            try {
                if (!"".equals(users)) {
                    //如果相关人员不为空的话，可以实例化后，发送邮件
                    String[] ary = users.split(",");
                    for (String s : ary) {
                        toUserDb = toUserDb.getUserDb(s);
                        toNick = MimeUtility.encodeText(toUserDb.getRealName());
                        if (SMSFactory.isUseSMS()) {
                            IMsgUtil imu = SMSFactory.getMsgUtil();
                            if (imu != null) {
                                try {
                                    imu.send(toUserDb, content, MessageDb.SENDER_SYSTEM);
                                } catch (ErrMsgException ex1) {
                                    ex1.printStackTrace();
                                }
                            }
                        }
                        // 发送信息
                        try {
                            md.sendSysMsg(toUserDb.getName(), title, content);
                        } catch (ErrMsgException ex2) {
                            LogUtil.getLog(getClass()).error(ex2);
                        }
                        if (flowNotifyByEmail) {
                            if (!"".equals(StrUtil.getNullStr(toUserDb.getEmail()))) {
                                sendmail.initMsg(toUserDb.getEmail(), fromNick, title, content, true);
                                sendmail.send();
                                sendmail.clear();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                return new Result<>(false, e.getMessage());
            }
        }
        return new Result<>(re);
    }

    @ApiOperation(value = "列出我的代理", notes = "列出我的代理", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/listMine", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> listMine(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize, @RequestParam(required = false, defaultValue = "") String field, @RequestParam(required = false, defaultValue = "desc") String order) {
        String myname = authUtil.getUserName();
        String op = ParamUtil.get(request, "op");
        String by = ParamUtil.get(request, "by");
        String what = ParamUtil.get(request, "what");
        String typeCode = ParamUtil.get(request, "typeCode");

        if ("".equals(field)) {
            field = "check_date";
        }

        String sql = "select m.id from flow_my_action m where checker<>user_name and checker=" + StrUtil.sqlstr(myname);
        if ("search".equals(op)) {
            if ("title".equals(by)) {
                if (!"".equals(typeCode)) {
                    sql = "select m.id from flow f,flow_my_action m where f.id=m.flow_id and f.type_code=" + StrUtil.sqlstr(typeCode) + " and f.title like " + StrUtil.sqlstr("%" + what + "%") + " and m.checker<>m.user_name and m.checker=" + StrUtil.sqlstr(myname);
                } else {
                    sql = "select m.id from flow f,flow_my_action m where f.id=m.flow_id and f.title like " + StrUtil.sqlstr("%" + what + "%") + " and m.checker<>m.user_name and m.checker=" + StrUtil.sqlstr(myname);
                }
            }
        }
        sql += " order by " + field + " " + order;

        JSONArray ary = new JSONArray();


        MyActionDb mad = new MyActionDb();
        ListResult lr = mad.listResult(sql, page, pageSize);
        java.util.Iterator ir = lr.getResult().iterator();
        com.redmoon.oa.person.UserMgr um = new com.redmoon.oa.person.UserMgr();
        WorkflowDb wfd2 = new WorkflowDb();
        while (ir.hasNext()) {
            mad = (MyActionDb) ir.next();

            JSONObject json = new JSONObject();
            WorkflowDb wfd = wfd2.getWorkflowDb((int) mad.getFlowId());
            String userName = wfd.getUserName();
            String userRealName = "";
            if (userName != null) {
                UserDb user = um.getUserDb(wfd.getUserName());
                userRealName = user.getRealName();
            }
            json.put("id", wfd.getId());
            json.put("realName", userRealName);
            json.put("levelImg", WorkflowMgr.getLevelImg(request, wfd));
            boolean isExpired = false;
            java.util.Date chkDate = mad.getCheckDate();
            if (chkDate==null) {
                chkDate = new java.util.Date();
            }
            if (DateUtil.compare(chkDate, mad.getExpireDate())==1) {
                isExpired = true;
            }
            json.put("isExpired", isExpired);
            json.put("receiveDate", DateUtil.format(mad.getReceiveDate(), "yy-MM-dd HH:mm"));
            json.put("checkDate", DateUtil.format(mad.getCheckDate(), "yy-MM-dd HH:mm"));
            json.put("expireDate", DateUtil.format(mad.getExpireDate(), "yy-MM-dd HH:mm"));
            json.put("origHandler", um.getUserDb(mad.getUserName()).getRealName());
            json.put("proxy", userRealName);
            json.put("status", WorkflowActionDb.getStatusName(mad.getActionStatus()));
            ary.add(json);
        }
        return new Result<>(ary);
    }

    @ApiOperation(value = "列出被代理的", notes = "列出被代理的", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/listProxy", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    public Result<Object> listProxy(String op, String by, @RequestParam(required = false, defaultValue = "") String what, String typeCode, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize, @RequestParam(required = false, defaultValue = "check_date") String field, @RequestParam(required = false, defaultValue = "desc") String order) {
        String myname = authUtil.getUserName();
        String sql = "select m.id from flow_my_action m where checker<>user_name and checker<>'' and checker is not null and user_name=" + StrUtil.sqlstr(myname);
        if ("search".equals(op)) {
            if ("title".equals(by)) {
                if (!"".equals(typeCode)) {
                    sql = "select m.id from flow f,flow_my_action m where f.id=m.flow_id and f.type_code=" + StrUtil.sqlstr(typeCode) + " and f.title like " + StrUtil.sqlstr("%" + what + "%") +  " and m.checker<>m.user_name and m.checker is not null and m.checker<>'' and m.user_name=" + StrUtil.sqlstr(myname);
                }
                else {
                    sql = "select m.id from flow f,flow_my_action m where f.id=m.flow_id and f.title like " + StrUtil.sqlstr("%" + what + "%") +  " and m.checker<>m.user_name and m.checker is not null and m.checker<>'' and m.user_name=" + StrUtil.sqlstr(myname);
                }
            }
        }
        sql += " order by " + field + " " + order;

        JSONArray ary = new JSONArray();
        MyActionDb mad = new MyActionDb();
        ListResult lr = mad.listResult(sql, page, pageSize);
        java.util.Iterator ir = lr.getResult().iterator();
        com.redmoon.oa.person.UserMgr um = new com.redmoon.oa.person.UserMgr();
        WorkflowDb wfd2 = new WorkflowDb();
        while (ir.hasNext()) {
            mad = (MyActionDb) ir.next();

            JSONObject json = new JSONObject();
            WorkflowDb wfd = wfd2.getWorkflowDb((int) mad.getFlowId());
            String userName = wfd.getUserName();
            String userRealName = "";
            if (userName != null) {
                UserDb user = um.getUserDb(wfd.getUserName());
                userRealName = user.getRealName();
            }
            json.put("id", wfd.getId());
            json.put("title", wfd.getTitle());
            json.put("realName", userRealName);
            json.put("level", wfd.getLevel());
            boolean isExpired = false;
            java.util.Date chkDate = mad.getCheckDate();
            if (chkDate==null) {
                chkDate = new java.util.Date();
            }
            if (DateUtil.compare(chkDate, mad.getExpireDate())==1) {
                isExpired = true;
            }
            json.put("isExpired", isExpired);
            json.put("receiveDate", DateUtil.format(mad.getReceiveDate(), "yy-MM-dd HH:mm"));
            json.put("checkDate", DateUtil.format(mad.getCheckDate(), "yy-MM-dd HH:mm"));
            json.put("expireDate", DateUtil.format(mad.getExpireDate(), "yy-MM-dd HH:mm"));
            json.put("processor", um.getUserDb(mad.getChecker()).getRealName());
            json.put("status", WorkflowActionDb.getStatusName(mad.getActionStatus()));
            ary.add(json);
        }
        return new Result<>(ary);
    }


}
